<?php
/*
 * glbpay_application_top.php
 *
 */
  define('IS_ADMIN_FLAG', false);

  error_reporting(0);//禁用错误报告
  $show_all_errors = true;
  $current_page_base = 'glbpayipn';

  define('PAGE_PARSE_START_TIME', microtime());
  error_reporting(E_ALL & ~E_NOTICE);

  @ini_set("arg_separator.output","&");

  //引用文件
  if (file_exists('includes/local/configure.php')) {
    include(DIR_WS_INCLUDES . '/local/configure.php');
  }
  if (file_exists('includes/configure.php')) {
    include('includes/configure.php');
  }
  require('includes/classes/class.base.php');
  require('includes/classes/class.notifier.php');
  require('includes/classes/class.phpmailer.php');
  require('includes/classes/class.smtp.php');

  $zco_notifier = new notifier();

  //建立数据库连接
  require('includes/classes/db/' .DB_TYPE . '/query_factory.php');
  $db = new queryFactory();
  if ( !$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE, USE_PCONNECT, false) ) {
    die('Cannot connect to database. Please notify webmaster.');
    exit;
  }

  //设置请求类型，ssl或nossl
  $request_type = (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1' || strstr(strtoupper($_SERVER['HTTP_X_FORWARDED_BY']),'SSL') || strstr(strtoupper($_SERVER['HTTP_X_FORWARDED_HOST']),'SSL'))  ? 'SSL' : 'NONSSL';


  require(DIR_WS_INCLUDES . 'filenames.php');
  require(DIR_WS_INCLUDES . 'database_tables.php');
  require(DIR_WS_FUNCTIONS . 'compatibility.php');
  require(DIR_WS_CLASSES . 'cache.php');
  $zc_cache = new cache;

  if ($za_dir = @dir(DIR_WS_INCLUDES . 'extra_datafiles')) {
    while ($zv_file = $za_dir->read()) {
      if (preg_match('/\.php$/', $zv_file) > 0) {
        require(DIR_WS_INCLUDES . 'extra_datafiles/' . $zv_file);
      }
    }
    $za_dir->close();
  }
//从数据库中获取配置，并设置为常量，允许缓存，缓存时间是150
  $configuration = $db->Execute('select configuration_key , configuration_value
                                 from ' . TABLE_CONFIGURATION, '', true, 150);
  while (!$configuration->EOF) {
    define($configuration->fields['configuration_key'], $configuration->fields['configuration_value']);
    $configuration->MoveNext();
  }
  if (file_exists(DIR_WS_CLASSES . 'db/' . DB_TYPE . '/define_queries.php')) {
    include(DIR_WS_CLASSES . 'db/' . DB_TYPE . '/define_queries.php');
  }

  require(DIR_WS_FUNCTIONS . 'functions_general.php');
  require(DIR_WS_FUNCTIONS . 'html_output.php');
  require(DIR_WS_FUNCTIONS . 'functions_email.php');
  require(DIR_WS_FUNCTIONS . 'functions_ezpages.php');
  include(DIR_WS_MODULES . 'extra_functions.php');


 //设置顶级域名
  $http_domain = zen_get_top_level_domain(HTTP_SERVER);//获得地址的顶级域名
  $https_domain = zen_get_top_level_domain(HTTPS_SERVER);
  $current_domain = (($request_type == 'NONSSL') ? $http_domain : $https_domain);
  if (SESSION_USE_FQDN == 'False') $current_domain = '.' . $current_domain;

  require(DIR_WS_CLASSES . 'shopping_cart.php');
  require(DIR_WS_CLASSES . 'navigation_history.php');
  require(DIR_WS_FUNCTIONS . 'sessions.php');

  zen_session_name('zenid');
  zen_session_save_path(SESSION_WRITE_DIRECTORY);

  session_set_cookie_params(0, '/', (zen_not_null($current_domain) ? $current_domain : ''));

   if (isset($_POST[zen_session_name()])) {
     zen_session_id($_POST[zen_session_name()]);
   } elseif ( ($request_type == 'SSL') && isset($_GET[zen_session_name()]) ) {
     zen_session_id($_GET[zen_session_name()]);
   }
  $session_started = false;
  if (SESSION_FORCE_COOKIE_USE == 'True') {
    zen_setcookie('cookie_test', 'please_accept_for_session', time()+60*60*24*30, '/', (zen_not_null($current_domain) ? $current_domain : ''));

    if (isset($_COOKIE['cookie_test'])) {
      zen_session_start();
      $session_started = true;
    }
  } elseif (SESSION_BLOCK_SPIDERS == 'True') {
    $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $spider_flag = false;

    if (zen_not_null($user_agent)) {
      $spiders = file(DIR_WS_INCLUDES . 'spiders.txt');

      for ($i=0, $n=sizeof($spiders); $i<$n; $i++) {
        if (zen_not_null($spiders[$i])) {
          if (is_integer(strpos($user_agent, trim($spiders[$i])))) {
            $spider_flag = true;
            break;
          }
        }
      }
    }

    if ($spider_flag == false) {
      zen_session_start();
      $session_started = true;
    }
  } else {
    zen_session_start();
    $session_started = true;
  }
//将存储在数据库中的session值取出并解码
  if (!$_SESSION['customer_id']) {
    $sql = "SELECT * FROM " . TABLE_GLBPAYDEBIT_SESSION . " WHERE orders_id = '" . $order_id . "' order by unique_id desc limit 1";
    $stored_session = $db->Execute($sql);
    $_SESSION = unserialize(base64_decode($stored_session->fields['session_info']));
  }

  if (!$_SESSION['cart']) {
    $_SESSION['cart'] = new shoppingCart;
  }

  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();

  if (!$_SESSION['language'] || isset($_GET['language'])) {

    require(DIR_WS_CLASSES . 'language.php');
    $lng = new language();
    if (isset($_GET['language']) && zen_not_null($_GET['language'])) {
      $lng->set_language($_GET['language']);
    } else {
      $lng->get_browser_language();
      $lng->set_language(DEFAULT_LANGUAGE);
    }
    $_SESSION['language'] = $lng->language['directory'];
    $_SESSION['languages_id'] = $lng->language['id'];
  }

  $sql = "select template_dir
          from " . TABLE_TEMPLATE_SELECT .
         " where template_language = '0'";

  $template_query = $db->Execute($sql);

  $template_dir = $template_query->fields['template_dir'];

  $sql = "select template_dir
          from " . TABLE_TEMPLATE_SELECT .
         " where template_language = '" . $_SESSION['languages_id'] . "'";

  $template_query = $db->Execute($sql);

  if ($template_query->RecordCount() > 0) {
      $template_dir = $template_query->fields['template_dir'];
  }

  define('DIR_WS_TEMPLATE', DIR_WS_TEMPLATES . $template_dir . '/');
  define('DIR_WS_TEMPLATE_IMAGES', DIR_WS_TEMPLATE . 'images/');
  define('DIR_WS_TEMPLATE_ICONS', DIR_WS_TEMPLATE_IMAGES . 'icons/');

  require(DIR_WS_CLASSES . 'template_func.php');
  $template = new template_func(DIR_WS_TEMPLATE);

  if (file_exists(DIR_WS_LANGUAGES . $template_dir . '/' . $_SESSION['language'] . '.php')) {
      $template_dir_select = $template_dir . '/';
  } else {
    $template_dir_select = '';
  }


  include(DIR_WS_LANGUAGES . $template_dir_select . $_SESSION['language'] . '.php');
  include(DIR_WS_MODULES . 'extra_definitions.php');

// currency
  if (!$_SESSION['currency'] || isset($_GET['currency']) || ( (USE_DEFAULT_LANGUAGE_CURRENCY == 'true') && (LANGUAGE_CURRENCY != $_SESSION['currency']) ) ) {
    if (isset($_GET['currency'])) {
      if (!$_SESSION['currency'] = zen_currency_exists($_GET['currency'])) $_SESSION['currency'] = (USE_DEFAULT_LANGUAGE_CURRENCY == 'true') ? LANGUAGE_CURRENCY : DEFAULT_CURRENCY;
    } else {
      $_SESSION['currency'] = (USE_DEFAULT_LANGUAGE_CURRENCY == 'true') ? LANGUAGE_CURRENCY : DEFAULT_CURRENCY;
    }
  }


// navigation history
  if (!isset($_SESSION['navigation'])) {
    $_SESSION['navigation'] = new navigationHistory;
  }
  $_SESSION['navigation']->add_current_page();

  require(DIR_WS_CLASSES . 'boxes.php');


  require(DIR_WS_CLASSES . 'message_stack.php');
  $messageStack = new messageStack;

  require(DIR_WS_FUNCTIONS . 'whos_online.php');
  zen_update_whos_online();

  require(DIR_WS_FUNCTIONS . 'password_funcs.php');

  require(DIR_WS_CLASSES . 'split_page_results.php');

  require(DIR_WS_FUNCTIONS . 'banner.php');
  zen_activate_banners();
  zen_expire_banners();

  require(DIR_WS_FUNCTIONS . 'specials.php');
  zen_start_specials();
  zen_expire_specials();

  require(DIR_WS_FUNCTIONS . 'featured.php');
  zen_start_featured();
  zen_expire_featured();

  require(DIR_WS_FUNCTIONS . 'salemaker.php');
  zen_start_salemaker();
  zen_expire_salemaker();

  require(DIR_WS_CLASSES . 'breadcrumb.php');
  $breadcrumb = new breadcrumb;

  $breadcrumb->add(HEADER_TITLE_CATALOG, zen_href_link(FILENAME_DEFAULT));

?>
